﻿using System;
using System.Windows;
using Baosight.ZNH.ZMCommon;
using Baosight.ZNH.ZMCommon.ChildWindows;
using Baosight.Platform.Core.UI.Controls;
using Baosight.Platform.Core;

namespace Baosight.ZNH.ZMCM.UI.ChildWindows
{
    public partial class ArrivalRegisterStorage : EFViewCmes
    {
        public ArrivalRegisterStorage(string dialogType)
        {
            InitializeComponent();
            base.BeforeAction += OnBeforeAction;
            Init(dialogType);
        }
        /// <summary>
        /// 初始化父类EFViewCmesProperty的值，每次点击“确定”、“返回”时执行此方法，从新赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void OnBeforeAction(object sender, EventArgs e)
        {
            EFViewCmesProperty _view = new EFViewCmesProperty();
            _view.DataFields = this.spWindows.GetChildObjects<EFDataField>(null, true);
            base.ViewProperty = _view;
        }

        /// <summary>
        /// 初始化页面数据
        /// </summary>
        private void Init(string type)
        {
            type = type == null ? "insert" : type.ToLower();

            switch (type)
            {
                case "insert":
                    base.Mode = OperationMode.Insert;
                    this.fdDepotName.IsReadOnly = true;

                    this.fdMaterialName.IsReadOnly = true;
                    this.fdMetric.IsEnabled = false;
                    this.fdSpecModel.IsReadOnly = true;
                    this.fdNumber.IsReadOnly = true;
                    this.fdQuality.IsReadOnly = true;
                    break;
                case "update":
                    base.Mode = OperationMode.Update;
                    break;
                case "detail":
                    this.Commit.Visibility = Visibility.Collapsed;
                    BaseTools.EFDataFieldNotEnabled(this.spWindows.GetChildObjects<EFDataField>(null, true));
                    break;
                default:
                    break;
            }
        }
        
        /// <summary>
        /// 确定按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CommitClick(object sender, RoutedEventArgs e)
        {

            if (!String.IsNullOrEmpty(this.fdInvoice.Value.ToString()) && !String.IsNullOrEmpty(this.fdInvoiceSum.Value.ToString()))
            {
                this.fdSum.Value = this.fdInvoiceSum.GetDataValue();
            }
            base.OnCommitButtonClick("ZMCMF3", "insert", sender, e);
        }

        /// <summary>
        /// 办理入库时的判断
        /// </summary>
        /// <returns></returns>
        public override bool ThisValidateField()
        {
            //if (this.fdImportType.Value !=null && "一般入库".Equals(this.fdImportType.Value.ToString()))
            //{
            //   // if (String.IsNullOrEmpty(this.fdInvoice.Value.ToString()) || String.IsNullOrEmpty(this.fdInvoiceSum.Value.ToString())) 
            //   // {
            //        //base.OpenEFWindowAlert("入库类型为一般入库时，则发票号、发票金额必须填写", true);
            //        //this.fdInvoice.eRegex = "^[a-zA-Z0-9]{1,50}$";
            //        //this.fdInvoice.eValidateErrorPrompt = "输入1-50个字符，包括字母、数字";
            //        //this.fdInvoice.eLabel = "*发票号";
            //        //this.fdInvoice.Width = 227 ;
            //        //this.fdInvoiceSum.eRegex = "^(-?\\d{1,10})(\\.\\d{0,5})?$";
            //        //this.fdInvoiceSum.eLabel = "*发票金额";
            //        //this.fdInvoiceSum.Width = 227;
            //       // return false;
            //   // }
            //}
            
            return true;
        }

        /// <summary>
        /// 取消按钮
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void CancelClick(object sender, RoutedEventArgs e)
        {
            CmesWindows _window = this.Parent as CmesWindows;
            _window.DialogResult = false;
            _window.Close();
        }

        /// <summary>
        /// 根据物资编码的值，给其他文本框赋值
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void fdMaterialCode_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            if (e.NewValue == null && e.OldValue.Equals(""))
            {
                return;
            }

            EFDataField field = sender as EFDataField;
            CmesCustomView picker = field.Content as CmesCustomView;

            if (picker.ItemObject != null)
            {
                if (!picker.ItemObject.Equals(""))
                {
                    this.fdMaterialCode.Value = picker.ItemObject.GetType().GetProperty("fdMaterialCode").GetValue(picker.ItemObject, null) as string;
                    this.fdMaterialName.Value = picker.ItemObject.GetType().GetProperty("fdMaterialName").GetValue(picker.ItemObject, null) as string;
                    this.fdMetric.Value = picker.ItemObject.GetType().GetProperty("fdMetric").GetValue(picker.ItemObject, null) as string;
                    this.fdSpecModel.Value = picker.ItemObject.GetType().GetProperty("fdSpecModel").GetValue(picker.ItemObject, null) as string;
                    this.fdNumber.Value = picker.ItemObject.GetType().GetProperty("fdNumber").GetValue(picker.ItemObject, null) as string;
                    this.fdQuality.Value = picker.ItemObject.GetType().GetProperty("fdMaterialName").GetValue(picker.ItemObject, null) as string;

                    this.fdMaterialId.Value = picker.ItemObject.GetType().GetProperty("fdKeyId").GetValue(picker.ItemObject, null) as string;
                    this.fdMaterialInnerCode.Value = picker.ItemObject.GetType().GetProperty("fdMaterialInnerCode").GetValue(picker.ItemObject, null) as string;
                    this.fdMaterialType.Value = picker.ItemObject.GetType().GetProperty("fdMaterialType").GetValue(picker.ItemObject, null) as string;
                    this.fdPrice.Value = picker.ItemObject.GetType().GetProperty("fdPrice").GetValue(picker.ItemObject, null);

                }
                //没选中时赋予空字符串
                else
                {
                    this.fdMaterialCode.Value = "";
                    this.fdMaterialName.Value = "";
                    this.fdMetric.Value = "";
                    this.fdSpecModel.Value = "";
                    this.fdNumber.Value = "";
                    this.fdQuality.Value = "";

                    this.fdMaterialId.Value = "";
                    this.fdMaterialInnerCode.Value = "";
                    this.fdMaterialType.Value = "";
                    this.fdPrice.Value = 0;
                }
                
            }

        }

        private void fdImportType_ValueChanged(object sender, ValueChangedEventArgs e)
        {
            if (this.fdImportType.Value != null && "一般入库".Equals(this.fdImportType.Value.ToString()))
            {
                this.fdInvoice.eRegex = "^[a-zA-Z0-9]{1,50}$";
                this.fdInvoice.eValidateErrorPrompt = "输入1-50个字符，包括字母、数字";
                this.fdInvoice.eLabel = "*发票号";
                this.fdInvoice.Width = 220;
                this.fdInvoiceSum.eRegex = "^(-?\\d{1,10})(\\.\\d{0,5})?$";
                this.fdInvoiceSum.eLabel = "*发票金额";
                this.fdInvoiceSum.Width = 220;
                this.fdInvoice.IsReadOnly = false;
                this.fdInvoiceSum.IsReadOnly = false;
            }
            else if (this.fdImportType.Value != null && "估收入库".Equals(this.fdImportType.Value.ToString()))
            {
                this.fdInvoice.eRegex = "^[a-zA-Z0-9]{0,50}$";
                this.fdInvoice.eValidateErrorPrompt = "输入0-50个字符，包括字母、数字";
                this.fdInvoice.eLabel = "发票号";
                this.fdInvoice.Value = "";
                this.fdInvoice.IsReadOnly = true;
                this.fdInvoice.Width = 220;
                this.fdInvoiceSum.eRegex = "^(-?\\d{0,10})(\\.\\d{0,5})?$";
                this.fdInvoiceSum.eLabel = "发票金额";
                this.fdInvoiceSum.Width = 220;
                this.fdInvoiceSum.Value = "";
                this.fdInvoiceSum.IsReadOnly = true;
            }
        }
        
    }
}
